package com.koron.system.menu.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Opt;
import com.koron.bean.base.BaseQueryBean;
import com.koron.bean.system.menu.MenuBean;
import com.koron.bean.system.menu.MenuFuncBean;
import com.koron.bean.system.role.AppMenuJurisdictionBean;
import com.koron.bean.system.role.MenuJurisdictionBean;
import com.koron.bean.system.role.UserOwnJurisdictionBean;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.util.UUIDUtils;
import com.koron.common.core.business.system.tree.TreeUtil;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.system.common.util.MenuTreeVo;
import com.koron.system.menu.mapper.MenuMapper;
import com.koron.system.menu.service.MenuService;
import com.koron.system.staff.service.StaffService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName MenuServiceImpl
 * @description
 * @Author zhouj
 * @Date 2021/7/22 18:23
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuMapper menuMapper;

    @Transactional
    @Override
    public int insertMenu(MenuBean menuBean) {

        menuBean.setId(UUIDUtils.getUuid());
        int i = menuMapper.insertMenu(menuBean);
        List<MenuFuncBean> menuFuncList = menuBean.getMenuFuncList();
        if (!CollectionUtils.isEmpty(menuFuncList)) {
            for (int y = 0; y < menuFuncList.size(); y++) {
                MenuFuncBean menuFuncBean = menuFuncList.get(y);
                if (StringUtils.isEmpty(menuFuncBean.getId())) {
                    menuFuncBean.setId(CodeTools.getCode32());
                }
                menuFuncBean.setMenuId(menuBean.getId());
                menuFuncBean.setSort(y);
            }
            menuMapper.insertMenuFunc(menuFuncList);
        }
        return i;

    }

    @Transactional
    @Override
    public int updateMenu(MenuBean menuBean) {
        //先删除菜单按钮
        int i = menuMapper.updateMenu(menuBean);
        menuMapper.deleteMenuFuncByMenuId(menuBean.getId());
        List<MenuFuncBean> menuFuncList = menuBean.getMenuFuncList();
        if (!CollectionUtils.isEmpty(menuFuncList)) {
            for (int y = 0; y < menuFuncList.size(); y++) {
                MenuFuncBean menuFuncBean = menuFuncList.get(y);
                if (StringUtils.isEmpty(menuFuncBean.getId())) {
                    menuFuncBean.setId(CodeTools.getCode32());
                }
                menuFuncBean.setMenuId(menuBean.getId());
                menuFuncBean.setSort(y);
            }
            menuMapper.insertMenuFunc(menuFuncList);
        }
        return i;
    }

    @Transactional
    @Override
    public int deleteMenuById(String id) {
        menuMapper.deleteMenuFuncByMenuId(id);
        return menuMapper.deleteMenuById(id);
    }

    @Transactional
    @Override
    public int deleteMenuList(List<String> ids) {
        menuMapper.deleteMenuFuncList(ids);
        return menuMapper.deleteMenuList(ids);
    }

    @Override
    public List<MenuTreeVo> queryMenuTree(MenuBean menuBean) {

        List<MenuTreeVo> list = menuMapper.queryMenuTree(menuBean);

        List<MenuTreeVo> treeVoList = TreeUtil.getTree(list);

        return treeVoList;

    }

    private EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }

    @Autowired
    private StaffService staffservice;

    @Override
    public List<MenuTreeVo> queryAppMenuTree(MenuBean menuBean) {

        List<MenuTreeVo> list = menuMapper.queryAppMenuTree(menuBean);

        EamUser eamUser = staffservice.queryStaffInfo(getCurrUser().getAccount());

        List<String> menuIdlist = new ArrayList<>();
        List<AppMenuJurisdictionBean> appMenuJurisdictionBeans = Opt.ofNullable(eamUser)
                .map(EamUser::getUserOwnJurisdictionBean)
                .map(UserOwnJurisdictionBean::getAppMenuJurisdictionBeanList)
                .orElse(new ArrayList<>());

        if (CollUtil.isNotEmpty(appMenuJurisdictionBeans)) {
            AppMenuJurisdictionBean appMenuJurisdictionBean = appMenuJurisdictionBeans.get(0);
            List<MenuJurisdictionBean> menuJurisdictionBeans = Opt.ofNullable(appMenuJurisdictionBean).map(AppMenuJurisdictionBean::getMenuJurisdictionBeanList).orElse(new ArrayList<>());
            menuIdlist = menuJurisdictionBeans.stream().map(MenuJurisdictionBean::getMenuId).distinct().collect(Collectors.toList());
        }
        for (MenuTreeVo menuTreeVo : list) {
            if (menuIdlist.contains(menuTreeVo.getId())) {
                menuTreeVo.setHasPermission(1);
            } else{
                menuTreeVo.setHasPermission(0);
            }
        }

        List<MenuTreeVo> treeVoList = TreeUtil.getTree(list);

        return treeVoList;

    }


    @Override
    public List<MenuTreeVo> queryLineAppMenuTree(MenuBean menuBean) {

        List<MenuTreeVo> list = menuMapper.queryLineAppMenuTree(menuBean);

        EamUser eamUser = staffservice.queryStaffInfo(getCurrUser().getAccount());

        List<String> menuIdlist = new ArrayList<>();
        List<AppMenuJurisdictionBean> appMenuJurisdictionBeans = Opt.ofNullable(eamUser)
                .map(EamUser::getUserOwnJurisdictionBean)
                .map(UserOwnJurisdictionBean::getAppMenuJurisdictionBeanList)
                .orElse(new ArrayList<>());

        if (CollUtil.isNotEmpty(appMenuJurisdictionBeans)) {
            AppMenuJurisdictionBean appMenuJurisdictionBean = appMenuJurisdictionBeans.get(0);
            List<MenuJurisdictionBean> menuJurisdictionBeans = Opt.ofNullable(appMenuJurisdictionBean).map(AppMenuJurisdictionBean::getMenuJurisdictionBeanList).orElse(new ArrayList<>());
            menuIdlist = menuJurisdictionBeans.stream().map(MenuJurisdictionBean::getMenuId).distinct().collect(Collectors.toList());
        }
        for (MenuTreeVo menuTreeVo : list) {
            if (menuIdlist.contains(menuTreeVo.getId())) {
                menuTreeVo.setHasPermission(1);
            } else{
                menuTreeVo.setHasPermission(0);
            }
        }

        List<MenuTreeVo> treeVoList = TreeUtil.getTree(list);

        return treeVoList;

    }

    @Override
    public MenuBean querySingleMenu(MenuBean menuBean) {
        return menuMapper.querySingleMenu(menuBean);
    }

    @Override
    public List<MenuBean> queryMenuList(MenuBean menuBean, BaseQueryBean queryBean) {
        List<MenuBean> menuBeans = menuMapper.queryMenuList(menuBean, queryBean);
        return menuBeans;
    }

    @Override
    public Long queryMenuListCount(MenuBean menuBean) {
        return menuMapper.queryMenuListCount(menuBean);
    }

    @Override
    public MenuBean queryByCode(String code) {
        return menuMapper.queryByCode(code);
    }
}
